package com.example.ujsfish.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.example.ujsfish.R;
import com.example.ujsfish.model.DeviceLocateBody;
import com.example.ujsfish.model.ship.DeviceLocate;
import com.example.ujsfish.model.ship.PoolName;
import com.example.ujsfish.utils.AppNetService;
import com.example.ujsfish.utils.DeviceName;
import com.example.ujsfish.utils.RetrofitManager;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class ChoseDevice extends AppCompatActivity implements AdapterView.OnItemSelectedListener {

    Spinner spinner_device;
    Button btn_choseDevice;
    Button btn_jump;

    String deviceId;
    RetrofitManager myRetrofitManager = RetrofitManager.getInstance();
    Retrofit myRetrofit = RetrofitManager.getRetrofit();
    AppNetService appNetService = myRetrofitManager.getApiService(AppNetService.class);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.chose_device_activity);
        spinner_device = findViewById(R.id.spinner_device);
        btn_choseDevice = findViewById(R.id.btn_choseDevice);
        btn_jump = findViewById(R.id.btn_jumpToManual);

        SharedPreferences preferences = getSharedPreferences("data",MODE_PRIVATE);
        String userName = preferences.getString("userName","");

        btn_choseDevice.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DeviceName.getDeviceInstance().getDeviceId().clear();
                DeviceName.getDeviceInstance().getDeviceId().add(deviceId);
                System.out.println("当前设备id：" + DeviceName.getDeviceInstance().getDeviceId());
                Toast.makeText(getApplication(),"设置成功",Toast.LENGTH_SHORT).show();
            }
        });

        btn_jump.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (DeviceName.getDeviceInstance().getDeviceId().isEmpty()){
                    DeviceName.getDeviceInstance().getDeviceId().clear();
                    DeviceName.getDeviceInstance().getDeviceId().add(deviceId);
                    System.out.println("当前设备id：" + DeviceName.getDeviceInstance().getDeviceId());
                    Toast.makeText(getApplication(),"设置成功",Toast.LENGTH_SHORT).show();
                }
                Intent intent = new Intent(ChoseDevice.this,CmManual.class);
                startActivity(intent);
            }
        });

        Call<PoolName> deviceLocate = appNetService.postFindDeviceBU(userName);
        deviceLocate.enqueue(new Callback<PoolName>() {
            @Override
            public void onResponse(Call<PoolName> call, Response<PoolName> response) {
                PoolName deviceBody = response.body();
                List<String> deviceLocateList = deviceBody.getData();
                initSpinner(deviceLocateList);
                System.out.println(deviceLocateList.toString());
                btn_jump.setEnabled(true);
            }

            @Override
            public void onFailure(Call<PoolName> call, Throwable t) {

            }
        });

    }

    private void initSpinner(List<String> deviceLocateList) {
        List<String> idList = new ArrayList<>();
        for (String device :
                deviceLocateList) {
            idList.add(device);
        }
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, idList);
        //设置样式
        arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        //加载适配器
        spinner_device.setAdapter(arrayAdapter);
        //为Spinner设置选中事件监听器
        spinner_device.setOnItemSelectedListener(ChoseDevice.this);
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        String data = parent.getItemAtPosition(position).toString();
        System.out.println("id" + data);
        switch (parent.getId()){
            case R.id.spinner_device:
                deviceId = data;
                break;
            default:break;
        }
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }
}